package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplate;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplate.Column;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateCharging;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateChargingRegion;
import com.ecoolex.chinadutyfree.retailer.pojo.AddFreightTemplateDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditFreightTemplateChargingDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditFreightTemplateDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.QueryFreightTemplateDTO;
import com.ecoolex.chinadutyfree.retailer.ws.DeliveryService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyType;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyTypeService;
import com.ecoolex.framework.modularize.api.service.region.Region;
import com.ecoolex.framework.modularize.api.service.region.RegionService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.google.common.collect.Lists;


@GraphQL2Module(type = Type.Admin, description = "运费模板管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,
	key = FreightTemplateGraphql.AUTH,
	name = "运费模板管理")
public class FreightTemplateGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-FREIGHT-TEMPLATE";

	@Reference
	private DeliveryService service;

	@Autowired
	private CurrencyTypeService currencyTypeService;

	@Autowired
	private RegionService regionService;

	@TypeField(type = FreightTemplate.class, field = "currencyType", description = "货币")
	public CurrencyType currencyType(@Source FreightTemplate source) {
		return currencyTypeService.get(source.getCurrencyType());
	}

	@TypeField(type = FreightTemplate.class, field = "chargingList", description = "计费集合")
	public List<FreightTemplateCharging> chargingList(@Source FreightTemplate source) {
		return service.queryFreightTemplateChargingByFreightTemplateId(source.getFreightTemplateId());
	}

	@TypeField(type = FreightTemplateCharging.class, field = "regionList", description = "区域集合")
	public List<FreightTemplateChargingRegion> chargingList(@Source FreightTemplateCharging source, Integer size) {
		return service.queryFreightTemplateChargingRegionByChargingId(source.getChargingId(), size);
	}

	@TypeField(type = FreightTemplateCharging.class, field = "countRegionNumber", description = "统计区域数量")
	public Long countRegionNumber(@Source FreightTemplateCharging source) {
		return service.countFreightTemplateChargingRegionByChargingId(source.getChargingId());
	}

	@TypeField(type = FreightTemplateChargingRegion.class, field = "region", description = "区域信息")
	public Region region(@Source FreightTemplateChargingRegion source) {
		return regionService.get(source.getRegionId());
	}

	private static final String AUTH_GET = AUTH + "-GET";

	@QueryField(field = "freightTemplate", description = "获取快递模板详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取快递模板详情")
	public FreightTemplate freightTemplate(@NonNull Integer freightTemplateId, AdminMemberAuthority user) {
		user.assertLogin();
		return service.getFreightTemplateById(freightTemplateId);
	}

	@QueryField(field = "freightTemplateChargingRegionList", description = "获取运费模板计费区域集合")
	public List<FreightTemplateChargingRegion> freightTemplateChargingRegionList(@NonNull Integer chargingId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.queryFreightTemplateChargingRegionByChargingId(chargingId, null);
	}

	private static final String AUTH_QUERY = AUTH + "-QUERY";

	@QueryField(field = "freightTemplateList", description = "查询快递模板列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询快递模板列表")
	public Pagination<FreightTemplate> queryFreightTemplate(@NonNull QueryFreightTemplateDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
		user.assertLogin();
		return service.queryFreightTemplate(query, sorts, page);
	}

	private static final String AUTH_ADD = AUTH + "-ADD";

	@MutationField(field = "addFreightTemplate", description = "添加运费模板")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加运费模板")
	public FreightTemplate addFreightTemplate(@NonNull AddFreightTemplateDTO add, @NonNull List<EditFreightTemplateChargingDTO> chargingList, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return service.addFreightTemplate(add, chargingList, user.userName());
	}

	private static final String AUTH_EDIT = AUTH + "-EDIT";

	@MutationField(field = "editFreightTemplate", description = "编辑运费模板")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑运费模板")
	public FreightTemplate editFreightTemplate(@NonNull EditFreightTemplateDTO edit, @NonNull List<EditFreightTemplateChargingDTO> chargingList, AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		FreightTemplate record = service.getFreightTemplateById(edit.getFreightTemplateId());
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "运费模板不存在");
		// 复制输入值过去
		BeanUtil.copy(edit,
			record,
			Lists.newArrayList(Column.pricingModel,
				Column.currencyType,
				Column.deleted,
				Column.createUser,
				Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return service.editFreightTemplate(record, chargingList, user.userName());
	}

	private static final String AUTH_DEL = AUTH + "-DEL";

	@MutationField(field = "delFreightTemplate", description = "删除运费模板")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除运费模板")
	public Boolean delFreightTemplate(@NonNull Integer freightTemplateId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		FreightTemplate record = service.getFreightTemplateById(freightTemplateId);
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "运费模板不存在");
		service.deleteFreightTemplate(record, user.userName());
		return true;
	}
}

